package org.september.pisces.sms.controller;

import org.september.core.exception.BusinessException;
import org.september.pisces.sms.entity.SmsTemplate;
import org.september.pisces.sms.service.SmsTmplService;
import org.september.pisces.user.permission.service.OperationLogService;
import org.september.simpleweb.controller.BaseController;
import org.september.simpleweb.model.ResponseVo;
import org.september.smartdao.model.Page;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;

/**
 * 短信模板管理
 */
@Controller
public class SmsTmplController extends BaseController {

    @Autowired
    private SmsTmplService smsService;
    
    @Autowired
    private OperationLogService operationLogService;

    @RequestMapping(value = List_Page)
    public ModelAndView smsTemplateList() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = List_Data)
    public ResponseVo<Page<SmsTemplate>> listSmsTemplateData(Page<SmsTemplate> page, SmsTemplate vo) throws Exception {
        ParamMap pm = new ParamMap(vo);
        page = smsService.getCommonDao().findPageByParams(SmsTemplate.class, page, "PiscesSms.listSmsTemplate", pm);
        return ResponseVo.<Page<SmsTemplate>>BUILDER().setData(page).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Add_Page)
    public ModelAndView addSmsTemplate() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Add_Action)
    public ResponseVo<String> doAddSmsTemplate(SmsTemplate tmpl) throws Exception {
    	smsService.save(tmpl);
        operationLogService.addLog("更新了短信模板【" + tmpl.getName() + "】");
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Edit_Page)
    public ModelAndView editSmsTemplate(Long id) throws Exception {
        ModelAndView mv = new ModelAndView();
        SmsTemplate po = smsService.getCommonDao().get(SmsTemplate.class, id);
        mv.addObject("tmpl", po);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Edit_Action)
    public ResponseVo<String> doUpdateSmsTemplate(@Valid SmsTemplate po) throws Exception {
    	smsService.update(po);
        operationLogService.addLog("更新了短信模板【" + po.getName() + "】");
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }
    
    @ResponseBody
    @RequestMapping(value = Delete_Action)
    public ResponseVo<String> deleteTemplate(Long id) throws Exception {
    	SmsTemplate po = smsService.getCommonDao().get(SmsTemplate.class, id);
        if (po == null) {
            throw new BusinessException("数据不存在或已删除");
        }
        smsService.delete(po);
        operationLogService.addLog("删除了短信模板【" + po.getName() + "】");
        return ResponseVo.<String>BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }
    
    private static final String Mapping_Prefix = "/pisces/sms/tmpl";
	public static final String List_Page = Mapping_Prefix+"/smsTemplateList";
	public static final String List_Data = Mapping_Prefix+"/listSmsTemplateData";
	
	public static final String Add_Page = Mapping_Prefix+"/addSmsTemplate";
	public static final String Add_Action = Mapping_Prefix+"/doAddSmsTemplate";
	
	public static final String Edit_Page = Mapping_Prefix+"/editSmsTemplate";
	public static final String Edit_Action = Mapping_Prefix+"/doUpdateSmsTemplate";
	public static final String Delete_Action = Mapping_Prefix+"/deleteTemplate";
}
